package top.dingyingying.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.dingyingying.springboot3.base.PageBean;
import top.dingyingying.springboot3.base.R;
import top.dingyingying.springboot3.dao.TbSoftwareDAO;
import top.dingyingying.springboot3.dao.TbSoftwareTypeDAO;
import top.dingyingying.springboot3.entity.TbSoftware;
import top.dingyingying.springboot3.entity.TbSoftwareType;
import top.dingyingying.springboot3.service.TbSoftwareService;
import top.dingyingying.springboot3.vo.PageVo;

@Service
@RequiredArgsConstructor
@Slf4j
public class TbSoftwareServiceimpl implements TbSoftwareService {
    private final TbSoftwareDAO tbSoftwareDAO;
    private final TbSoftwareTypeDAO tbSoftwareTypeDAO;


    @Override
    public R<String> insert(TbSoftware tbSoftware) {
        TbSoftwareType check = tbSoftwareTypeDAO.selectById(tbSoftware.getStid());
        if (check == null) {
            return R.fail("软件类型错误", null);
        }
        int i = tbSoftwareDAO.insert(tbSoftware);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }

    @Override
    public R<PageVo<TbSoftware>> query(TbSoftware tbSoftware, PageBean pageBean) {
        IPage<TbSoftware> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbSoftware> wrapper = new QueryWrapper<>();
        wrapper.eq(tbSoftware.getStid() != null,
                "stid", tbSoftware.getStid());

        wrapper.like(tbSoftware.getSname() != null,
                "sname", tbSoftware.getSname());
        page = tbSoftwareDAO.selectPage(page, wrapper);
        return R.ok(page);

    }



    @Override
    public R<TbSoftware> queryById(Integer id) {
    TbSoftware tbSoftware = tbSoftwareDAO.selectById(id);
     return tbSoftware == null ?
             R.fail("查询失败", null) :
             R.ok("查询成功", tbSoftware);
 }

 @Override
 public R<String> update(TbSoftware tbSoftware){
    TbSoftware check = tbSoftwareDAO.selectById(tbSoftware.getStid());
    if (check == null) {
        return R.fail("软件类型错误", null);
    }
    int i = tbSoftwareDAO.updateById(tbSoftware);
    return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
 }

}

